package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.domain.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    //添加预约设置
    @Override
    public boolean add(List<OrderSetting> list) {
        boolean flag=true;
        //判断参数是否为null或空
        if (list!=null&&list.size()>0){
            //遍历集合
            for (OrderSetting orderSetting : list) {
                //调用Dao方法，通过时间进行查询，返回值为OrderSetting
                OrderSetting order = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                //判断返回值是否为null
                if (order!=null){
                    //不为null，判断预约设置人数是否小于已预约人数
                    if (order.getReservations()>orderSetting.getNumber()){
                        flag=false;
                    }else {
                        orderSettingDao.editNumberByOrderDate(orderSetting);
                    }
                }else {
                    //不为null，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
        return flag;
    }
    //查询预约设置
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接字符串，获取查询的时间范围
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        //创建map集合，将查询的时间范围添加进去
        HashMap map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        //调用Dao中的方法，返回OrderSetting的list集合
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        //创建list集合，元素为map
        List<Map> data = new ArrayList<>();
        //遍历返回集合
        for (OrderSetting orderSetting : list) {
            //创建map集合
            Map ordersettingmap= new HashMap();
            //取出orderSetting对象中的元素，添加到map集合中
            ordersettingmap.put("date",orderSetting.getOrderDate().getDate());
            ordersettingmap.put("number",orderSetting.getNumber());
            ordersettingmap.put("reservations",orderSetting.getReservations());
            //将map集合添加到list集合中
            data.add(ordersettingmap);
        }
        return data;
    }
    //修改设置
    @Override
    public boolean editNumberByDate(OrderSetting orderSetting) throws Exception {
        boolean flag=true;
        //判断修改数据是否为0
        if (orderSetting.getNumber()!=0){
            //不为0，执行更新操作
            OrderSetting order = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            //判断返回值是否为null
            if (order!=null){
                //不为null，判断预约设置人数是否小于已预约人数
                if (order.getReservations()>orderSetting.getNumber()){
                    flag=false;
                }else {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }
            }else {
                //不为null，执行添加操作
                orderSettingDao.add(orderSetting);
            }
        }else {
            //为0，执行删除操作
            orderSettingDao.delete(orderSetting.getOrderDate());
        }
        return flag;
    }
}
